<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
  <head>
    <base href="../dashboard/www/" />
    <link rel="stylesheet" type="text/css" href="style/reset.css">
    <link rel="stylesheet" type="text/css" href="style/default.css">
    <script type="text/javascript" src="javascript/jquery/jquery.js"></script>
    <script type="text/javascript" src="javascript/jquery/jquery.json.js"></script>
    <script type="text/javascript">  //<![CDATA[

      /** ------------------------------------------------------------------ */
      $(document).ready(function() {
        //bootstrap();
        //single();
        dual();
        //linked();
      });

      /** ------------------------------------------------------------------ */
      function bootstrap() {
        var afrimesh_messages = {
          parents : [ "afrimesh" ],
          members : [ 1,2,4,5 ]
          //prev    : 0,
          //next    : 0
        };
        var afrimesh_messages_1 = {
          parents : [ "afrimesh.messages" ],
          data  : { subject : "Tyre Inflator For Sale",
                    url     : "http://foo.com/plink/plonk.html",
                    from    : "antoine",
                    tags    : ["kommetjie", "forsale"] },
          //prev    : 4,
          next    : 2
        };
        var afrimesh_messages_2 = {
          parents : [ "afrimesh.messages" ],
          data  : { subject :  "Caterpillar Payloader 950E (Newly imported)",
                    url   :  "bar.co.za",
                    from  :  "joe",
                    tags  : ["scarborough", "forsale"] },
          prev    : 1,
          next    : 4
        };
        var afrimesh_messages_4 = {
          parents : [ "afrimesh.messages" ],
          data  : { subject :  "Three Bedroom Apartment With Swimming Pool And Gym",
                    url   :  "http://bar.co.za/foo",
                    from  :  "joe",
                    tags  : ["scarborough", "housing"] },
          prev    : 2,
          next    : 5
        };
        var afrimesh_messages_5 = {
          parents : [ "afrimesh.messages" ],
          data  : { subject :  "l-cube iChatAgent[86439]: WARNING",
                    url   :  "http://l-cube/",
                    from  :  "syslogd@l-cube",
                    tags  : ["scarborough", "system"]  },
          prev    : 4
        };
        var values = set_memcached( { "afrimesh.messages"   : afrimesh_messages,
                                      "afrimesh.messages.1" : afrimesh_messages_1,
                                      "afrimesh.messages.2" : afrimesh_messages_2,
                                      "afrimesh.messages.4" : afrimesh_messages_4,
                                      "afrimesh.messages.5" : afrimesh_messages_5  } );
        $("div#test").append("<p>" + $.toJSON(values) + "</p>");
      };


      /** ------------- */
      get_memcached = function(query) { return memcached({ get : query }); }
      set_memcached = function(query) { return memcached({ set : query }); }
      function memcached(query) {
        var request = {};
        request.servers = "localhost";
        for (p in query) {
          request[p] = query[p];
        }
        $("div#requests").append("<p><b><i>" + request + "</i></b> " + $.toJSON(request) + "</p>");
        var xml = $.ajax({
            url         : "http://localhost/cgi-bin/village-bus-memcached",
            type        : "POST",
            contentType : "application/json",
            dataType    : "json",
            async       : false,
            data        : $.toJSON(request),
            success     : function(response) {
              $("div#responses").append("<p><b><i>" + response + "</i></b> " + $.toJSON(response) + "</p>");
              request.response = response[response.length - 1];
            }
          });
        return request.response;
      }

      function args_to_array(args) {
        var ret = [];
        for (var i = 0; i < args.length; i++) {
          ret.push(args[i]);
        }
        return ret;
      }
    //]]></script>
  </head>

  <body>
    <script>
      function single() {
        var records = get_memcached( { "1" : "afrimesh.messages.1", 
                                       "2" : "afrimesh.messages.2",
                                       "3" : "afrimesh.messages.4",
                                       "4" : "afrimesh.messages.5",
                                       "5" : "afrimesh.messages.6",
                                       "6" : "afrimesh.messages.7" } );
        $("div#test").append("<p>" + $.toJSON(records) + "</p>"); 
      };

      function dual() {
        var messages = get_memcached( { "messages" : "afrimesh.messages" } ).messages;
        $("div#test").append("<p><b>Messages:</b>" + $.toJSON(messages) + "</p>"); 
        var query = {};
        for (var id in messages.members) {
          query[id] = "afrimesh.messages." + messages.members[id];
        }
        $("div#test").append("<p><b>Query:</b> " + $.toJSON(query) + "</p>"); 
        var records = get_memcached(query);
        $("div#test").append("<p><b>Records:</b> " + $.toJSON(records) + "</p>"); 
      };

      function splice_message(message) {
      };

      function push_message(message) {
        var messages = get_memcached( { "messages" : "afrimesh.messages" } ).messages;
        if (messages.members.length) {
          var last_id = messages.members[messages.members.length - 1];
          var last_record = get_memcached( { "record" : "afrimesh.messages." + last_id } ).record;
          last_record.next = last_id + 1;
          messages.members.push(last_record.next);
          set_memcached( { "afrimesh.messages" : messages } );
          var q = {};
          q["afrimesh.messages." + last_id] = last_record;
          set_memcached(q);
          var record = {
            parents : [ "afrimesh.messages" ],
            data    : message,
            prev    : last_id
          };
          q = {};
          q["afrimesh.messages." + last_record.next] = record;
          set_memcached(q);            
        }
      };

      function linked() {
        var record = { next : 1 };
        while (record.next) {
          record = get_memcached( { "record" : "afrimesh.messages." + record.next } ).record;
          $("div#test").append("<p>" + $.toJSON(record.data) + "</p>"); 
        }
      };

      function do_it() {
        push_message({ subject : "dashboard su: antoine to root on /dev/ttyp0",
                      url     : "http://dashboard.7degrees.co.za/",
                      from    : "root@dashboard",
                      tags    : ["kommetjie", "system"]  });
        dual();
      };
    </script>
    <div id="container">
      <button onClick="do_it();"><p>do it<br/> now</p></button>
      <div id="test"      style="color:blue;" ><h2>Test Output     </h2></div><br/>
      <hr/>
      <div id="misc"      style="color:black;"><h2>Misc Output     </h2></div><br/>
      <hr/>
      <div id="requests"  style="color:red;"  ><h2>Server Requests </h2></div><br/>
      <div id="responses" style="color:green;"><h2>Server Responses</h2></div><br/>
    </div>
    
  </body>
</html>
